/*
 * =====================================================================================
 *
 *       Filename:  process.c
 *
 *    Description:  测试libubox process
 *
 *        Version:  1.0
 *        Created:  2019年09月10日 11时56分15秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "uloop.h"

void run_child()
{
    sleep(1);
    printf("child %d fearwell\n", getpid());
}

void restart_child(struct uloop_process *p)
{
    /* 被调用时子进程已经退出了1次 */
    printf("process cb\n");
}

int main(int argc, char **argv)
{
    if(uloop_init() < 0) {
        printf("uloop_init failed\n");
        return 0;
    }
    struct uloop_process *p = (struct uloop_process *)calloc(1, sizeof(*p));
    if(NULL == p) {
        perror("calloc()");
        goto this_exit;
    }
    p->cb = restart_child;
    p->pid = fork();
    if(p->pid == 0) {
        run_child();
        exit(0);
    }
    uloop_process_add(p);
    uloop_run();
this_exit:
    uloop_done();
    if(p) {
        free(p);
    }
    return 0;
}


